package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.invoke.SerializedLambda;

@RequestMapping("user")
@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public UserBean login(String account, String password,HttpServletRequest req) {
        UserBean user = userService.login(account, password);
        req.getSession().setAttribute("user",user);
        return user;
    }

    @RequestMapping("cutByItem")
    public IPage<UserBean> cutByItem(Integer pageNO, String name) {
        return userService.cutByItem(pageNO, name);
    }

    @RequestMapping("del")
    public String del(Integer id) {
        userService.del(id);
        return "ok";
    }

    @RequestMapping("findById")
    public UserBean findById(Integer id) {
        return userService.findById(id);
    }

    @RequestMapping("update")
    public String update(Integer id, Integer[] arr) {
        userService.update(5, arr);
        return "ok";
    }

    @RequestMapping("add")
    public String add(UserBean userBean, Integer[] arr) {
        userService.add(userBean, arr);
        return "ok";
    }
@RequestMapping("changePWD")
    public  String changePWD ( HttpServletRequest req, String password){
        UserBean user =(UserBean) req.getSession().getAttribute("user");
        userService.changePWD(user.getId(),password);
        return "ok";
}
}
